package com.yss.fengxin.work;

import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCompute;
import org.apache.ignite.Ignition;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder;

import java.util.Arrays;


public class IgniteHelloWorld {

    public static void main(String[] args){
        IgniteConfiguration cfg=new IgniteConfiguration();
        // Explicitly configure TCP discovery SPI to provide list of initial nodes
        // from the first cluster.

        TcpDiscoverySpi discoverySpi=new TcpDiscoverySpi();

        //discoverySpi.setLocalAddress("192.168.3.173");
        // Initial local port to listen to.
        discoverySpi.setLocalPort(47509);
        // Changing local port range. This is an optional action.
        //discoverySpi.setLocalPortRange(20);

        TcpDiscoveryVmIpFinder ipFinder=new TcpDiscoveryVmIpFinder();
        ipFinder.setAddresses(Arrays.asList("127.0.0.1:47500..47520"));
        // Overriding IP finder.
        discoverySpi.setIpFinder(ipFinder);
        // Overriding discovery SPI.
        cfg.setDiscoverySpi(discoverySpi);

        cfg.setClientMode(true);
        cfg.setIgniteInstanceName("ClientNode");


        // 对等类加载
        cfg.setPeerClassLoadingEnabled(true);

        //final Ignite ignite = Ignition.ignite();
        Ignite ignite = Ignition.start(cfg);
        // Limit broadcast to remote nodes only.
        IgniteCompute compute = ignite.compute(ignite.cluster().forRemotes());

        // Print out hello message on remote nodes in the cluster group.
        compute.broadcast(() -> System.out.println("Hello Node: " + ignite.cluster().localNode().metrics()));
       // PrintWork pw = new PrintWork();
       // compute.broadcast(pw);


       /*
        try(Ignite ignite = Ignition.start(cfg)){
            ignite.compute().broadcast(pw);
        }
        */
    }
}
